package com.peanut.baby.mvp.main.expert.qa;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.peanut.baby.R;
import com.peanut.baby.model.QA;
import com.peanut.baby.mvp.profile.ProfileActivity;
import com.peanut.baby.util.TimeUtil;
import com.peanut.devlibrary.imageloader.ImageLoader;
import com.peanut.devlibrary.widget.pullrecycle.BaseRecyclerAdapter;
import com.peanut.devlibrary.widget.pullrecycle.BaseViewHolder;

import java.util.List;

/**
 * Created by anonymouslystudio on 2017/7/12.
 */

public class QARecyclerAdapter extends BaseRecyclerAdapter<QA> {
    private OnItemViewClickListener listener;
    /**
     *
     * @param context
     * @param data
     */
    public QARecyclerAdapter(Context context, List<QA> data) {
        super(context,  data);
    }

    @Override
    protected void convert(BaseViewHolder holder, QA item, int position) {
        super.convert(holder, item, position);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_item_qa_list;
    }

    public void setOnClickListener(OnItemViewClickListener l){
        this.listener = l;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final QA item) {
        TextView   nick = holder.getView(R.id.qa_asker_nick);
        ImageView  avatar = holder.getView(R.id.qa_asker_avatar);
        TextView   date = holder.getView(R.id.qa_asker_date);
        TextView   status = holder.getView(R.id.qa_asker_status);
        TextView   lisCount = holder.getView(R.id.qa_listen_count);
        TextView   cmtCount = holder.getView(R.id.qa_cmt_count);
        final CheckBox    prasiedCount = holder.getView(R.id.qa_prasied_count);
        TextView   content = holder.getView(R.id.qa_content);
        TextView   category = holder.getView(R.id.qa_category);

        ImageLoader.getInstance().loadImageView(mContext,item.creatorAvatar,avatar);
        nick.setText(item.creatorNickname);
        date.setText(TimeUtil.getQAListDisplayTime(item.createTime));
        status.setText(item.getStatusString());
        lisCount.setText(item.listenCount+"");
        cmtCount.setText(item.commentCount+"");
        prasiedCount.setText(item.praiseCount+"");
        content.setText(item.description);
        category.setText(item.type);
        prasiedCount.setChecked(item.isPraised());

        if(item.isAnswered()){
            status.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
        }else{
            status.setTextColor(mContext.getResources().getColor(R.color.text_dark));
        }

        holder.getView(R.id.qa_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onViewItemClicked(v,item);
                }
            }
        });

        prasiedCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prasiedCount.setChecked(item.isPraised());
                if(listener != null){
                    listener.onViewPraiseClicked(item);
                }
            }
        });

        avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProfileActivity.start(mContext,item.creatorId+"");
            }
        });



    }

    public interface OnItemViewClickListener{
        void onViewItemClicked(View v, QA item);
        void onViewPraiseClicked(QA qa);
    }

}
